package srv

import (
	"fmt"
	"jgt-oper/src/bll"
	"jgt-oper/src/context"
)

// Enterprise 企业管理
type Enterprise struct {
	EnterpriseBll *bll.Enterprise `inject:""`
}

// Get 获取企业详情
func (a *Enterprise) Get(ctx *context.Context) error {
	item, err := a.EnterpriseBll.Get(ctx.Param("id"))
	if err != nil {
		return ctx.ResErrInternalServer(err)
	} else if item == nil {
		return ctx.ResErrBadRequest(fmt.Errorf("未找到企业数据"))
	}

	data := map[string]interface{}{
		"name":         item.Name,
		"contacter":    item.Contacter,
		"contact_tel":  item.ContactTel,
		"address":      item.Address,
		"category":     item.Category,
		"entry_time":   item.EntryTime,
		"logo":         item.Logo,
		"introduction": item.Introduction,
	}
	return ctx.ResSuccess(data)
}

// UpdatePanel 更新展板
func (a *Enterprise) UpdatePanel(ctx *context.Context) error {
	var reqData struct {
		IsOpen int `json:"is_open"`
	}

	if err := ctx.DecodeJSON(&reqData); err != nil {
		return ctx.ResErrBadRequest(err)
	}

	isOpen := 2
	if reqData.IsOpen == 1 {
		isOpen = 1
	}
	err := a.EnterpriseBll.UpdatePanel(ctx.Param("id"), isOpen)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}

	return ctx.ResOK()
}
